/* eslint-disable array-callback-return */
'use strict';

const Sequelize = require('sequelize');
const Op = Sequelize.Op;

const Service = require('egg').Service;
class AttributeService extends Service {
  // 获取规格key value
  async show(goods_id) {
    try {
      const { Sku } = this.app.model.Tables.Shop;
      const result = await Sku.findOne({
        where: {
          goods_id,
        },
      });
      if (result) {
        return this.ctx.response.success(result);
      }
      return this.ctx.response.error('找不到该商品规格信息');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }

  // 添加规格key
  async keyCreate(dataObj) {
    const { ctx, service } = this;
    const { AttributesKey } = this.app.model.Tables.Shop;
    console.log(dataObj, 'keyAttributesKey');
    try {
      //   const resultGoods = await Sku.findOne({
      //     where: {
      //       goods_id: dataObj.goods_id,
      //     },
      //   });
      //   if (resultGoods) {
      //     return ctx.response.success("已有该商品的规格信息");
      //   } else {
      const result = await AttributesKey.create(dataObj);
      return result;
      //     return ctx.response.success("添加成功");
      //   }
    } catch (error) {
      console.log(error);
      return ctx.response.error(
        error.message === 'Validation error' ? '已有该商品规格' : error.message
      );
    }
  }
  // 添加规格value
  async valueCreate(dataObj) {
    const { ctx, service } = this;
    const { AttributesValue } = this.app.model.Tables.Shop;
    const result = await AttributesValue.create(dataObj);
    try {
    } catch (error) {
      console.log(error);
      return ctx.response.error(
        error.message === 'Validation error' ? '已有该商品规格' : error.message
      );
    }
  }
  // 更新规格key value
  async update(goods_id, data) {
    const { ctx, service } = this;
    try {
      const { Sku } = this.app.model.Tables.Shop;
      const resultGoods = await Sku.findOne({
        where: {
          goods_id,
        },
      });

      if (resultGoods) {
        const resultG = await resultGoods.update({
          ...data,
        });
        return resultG
          ? ctx.response.success(resultG, '更新成功')
          : ctx.response.error(resultG, '更新失败');
      }
      return ctx.response.error('找不到该商品规格信息');
    } catch (error) {
      return ctx.response.error(
        error.message === 'Validation error' ? '已有该商品名称' : error.message
      );
    }
  }
  // 删除规格key value
  async destroy(goods_id) {
    const { ctx } = this;
    try {
      const { Sku } = this.app.model.Tables.Shop;
      const resultGoods = await Sku.destroy({
        where: {
          goods_id,
        },
      });
      return resultGoods > 0
        ? ctx.response.success('删除成功')
        : ctx.response.error('删除失败');
      // if (resultGoods) {
      //   // const resultG = await resultGoods.update({ status: '2' });
      //   return resultG ? (ctx.response.success('删除成功')) : (ctx.response.error(resultG, '更新失败'));
      // }
      // return ctx.response.error('找不到该商品信息');
      // return result > 0 ? (ctx.response.success('删除成功')) : (ctx.response.error('删除失败'));
    } catch (error) {
      return ctx.response.error(
        error.message === 'Validation error' ? '已有该商品名称' : error.message
      );
    }
  }
}

module.exports = AttributeService;
